$(function () {
     //input
     $(document).ready(function () {
         var callbacks_list = $('.demo-callbacks ul');
         $('.red-packet-num input,').on('ifCreated ifClicked ifChanged ifChecked ifUnchecked ifDisabled ifEnabled ifDestroyed', function (event) {
             callbacks_list.prepend('<li><span>#' + this.id + '</span> is ' + event.type.replace('if', '').toLowerCase() + '</li>');
         }).iCheck({
             checkboxClass: 'icheckbox_square-blue',
             radioClass: 'iradio_square-blue',
             increaseArea: '20%'
         });
     });

     //summary 物流方式展开关闭
     $(".logistics-text").click(function () {
         //$(this).addClass("current");
         $(this).toggleClass("current");
         $(this).parents(".order-i-nr").siblings(".logistics-select").fadeToggle(50);
         $(this).parents(".order-i-nr").siblings(".step-virtual").fadeOut(50);
         
         //控制物流方式选择项后是否显示 到达日期
         var timeEle = $(this).next('.logistics-time');
         if( $(this).hasClass("current") == true ){
             timeEle.html('').removeClass('current');
         }else{
             if( timeEle.attr('arrivalData') != undefined ){
                 timeEle.html( '( 预计到达日期：' + timeEle.attr('arrivalData') + ' )' ).addClass('current ');
            }
         }
     })
     //选择物流方式事件
     var wuliuChange = function(){
        $(".logistics-span span").click(function () {
            $(this).addClass("current").siblings().removeClass("current");
             
             //$(".logistics-tab").hide().eq($(".logistics-span span").index(this)).show();
             var texttab = $(this).text();
             $(this).parents(".logistics-select").siblings(".order-i-nr").find(".logistics-text span").text(texttab);

            /*
             if($(this).hasClass("current")){
               var texttime=$(".logistics-tab").eq($(".logistics-span span").index(this)).find(".tab-time").text()
               $(this).parents(".logistics-select").siblings(".order-i-nr").find(".logistics-time").text(texttime)
               $(this).parents(".logistics-select").siblings(".order-i-nr").find(".logistics-time").addClass("current");
             }else{
               $(this).parents(".logistics-select").siblings(".order-i-nr").find(".logistics-time").text();
               $(this).parents(".logistics-select").siblings(".order-i-nr").find(".logistics-time").removeClass("current");
             }
            */

             //初始化
             var costAndDate = $(this).parents('.logistics-select').find('.logistics-tab');
             costAndDate.html('');

             //运费
             var provinceid = $('.consignee-detail').attr('privinceid');
             var cityid = $('.consignee-detail').attr('cityid');
             var townid = $('.consignee-detail').attr('townid');
             var carrierId = $(this).parents(".logistics-select").prev(".order-i-nr").find('.carrier').val();
             var logisticsId = $(this).attr('id');
             var warehouseid = $(this).parents('.order-i-tit1').find('.order-i-border').attr('warehouseId');
             var weight = $(this).parents('.order-info').find('.shopName').attr('weight');
             var self = this;
             var params = {
                 warehouseid:warehouseid,
                 provinceid:provinceid,
                 cityid:cityid,
                 townid:townid,
                 carrierId:carrierId,
                 logisticsId:logisticsId,
                 weight:weight
             };
             $.get('/index/ajax/freightCost.html', params, function(rs){
                //$('.freightCost').text('￥'+rs);
                costAndDate.html('快递费用：'+ rs.cost +'元 <span class="tab-time">( 预计到达日期：'+ rs.arrivalData +' )</span>');
                $(self).parents('.logistics-select').prev('.order-i-nr').find('.logistics-time').attr('arrivalData',rs.arrivalData);
             });
         })
     }
     wuliuChange();
     
     $(".step-points").click(function () {
         $(this).parent(".step-points").toggleClass("current");
         $(".step-virtual").fadeToggle(50)
         $(".logistics-select").fadeOut(50)
     })
     //step info
     $(".step-info").hover(function () {
         $(".step-i-text").show()
     }, function () {
         $(".step-i-text").hide()
     })
     //step virtual
     $(".virtual-tit span").click(function () {
         $(this).addClass("current").siblings().removeClass("current");
         $(".virtual-tab").hide().eq($(".virtual-tit span").index(this)).show();
     })
     //red packet
     $(".red-packet-tit span").click(function () {
         $(this).addClass("current").siblings().removeClass("current");
         $(".red-packet-tab").hide().eq($(".red-packet-tit span").index(this)).show();
     })

     //cart bottom
     $(window).bind("scroll", function () {
        var top = $(this).scrollTop() + $(window).height() - 80;
        var t = $(".cart-ding").offset().top;
        if (top > t) {
            $('.cart-item-bottom').removeClass('cart-item-fixed');
        } else {
            $('.cart-item-bottom').addClass('cart-item-fixed');
        }
    });

    //承运商->物流方式
    $('.carrier').change(function(){
        var cid = $(this).val();
        //var selfObj = $(this);
        var wuliuListObj = $(this).parent('.order-i-nr').next('.logistics-select').find('.logistics-span'); //物流方式
        var costAndTimeObj = $(this).parent('.order-i-nr').next('.logistics-select').find('.logistics-tab'); //运费和预计到达时间

        //初始化
        wuliuListObj.html('');
        costAndTimeObj.html('');
        $(this).next('.step-logistics').find('.logistics-text span').text('请选择物流方式');
        $(this).next('.step-logistics').find('.logistics-time').html('').removeAttr('arrivaldata');
        //$('.freightCost').html('￥ 0.00');

        $.get('/index/ajax/logistics.html', {cid:cid}, function(rs){
            var wuliuHtml = '';
            if(rs){
                $.each(rs, function(index,item){
                    wuliuHtml += '<span id='+ item.id +'>'+ item.title +'<i></i></span>';
                });
                wuliuListObj.html(wuliuHtml);
                wuliuChange();
            }
        });
    });
})